﻿using HealthcareSystem.API.Read.Application.Command.Indicator;
using HealthcareSystem.API.Read.DTO.Indicator;
using HealthcareSystem.Domain.BasicSettings.Metrics;
using MediatR;
using Microsoft.Identity.Client;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Read.Application.CommandHandel.Metrics
{
    /// <summary>
    /// 指标请求命令
    /// </summary>
    public class MetricsCommandHandler : IRequestHandler<MetricsCommand, ApiResult<ApiPaging<List<MetricsDTO>>>>
    {
        /// <summary>
        /// 指标仓储
        /// </summary>
        private readonly IBaseRepository<Metricss> metricssRep;
        /// <summary>
        /// 指标分类仓储
        /// </summary>
        private readonly IBaseRepository<MetricsClass> metclassRep;
        /// <summary>
        /// 请求命令
        /// </summary>
        /// <param name="metricssRep">指标仓储</param>
        /// <param name="metclassRep">指标分类仓储</param>
        public MetricsCommandHandler(IBaseRepository<Metricss> metricssRep, IBaseRepository<MetricsClass> metclassRep)
        {
            this.metricssRep = metricssRep;
            this.metclassRep = metclassRep;
        }
        /// <summary>
        /// 请求处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<ApiPaging<List<MetricsDTO>>>> Handle(MetricsCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<List<MetricsDTO>>> result=new ApiResult<ApiPaging<List<MetricsDTO>>>();
            result.Code=ResultCode.Success;
            result.Message=ResultCode.查询成功.ToString();

            var metlist = from a in metricssRep.GetAll()
                          join b in metclassRep.GetAll() on a.MetricsClassId equals b.MetricsClassId
                          where !a.IsDeleted
                          select new MetricsDTO
                          {
                              MetricsId=a.MetricsId,
                              MetricsName=a.MetricsName,
                              MetricsCode=a.MetricsCode,
                              MetricsClassId=a.MetricsClassId,
                              IsDeleted=a.IsDeleted,
                              MetricsClassName=b.MetricsClassName,
                          };
            if (request.MetricsClassId != 0)
            {
                metlist=metlist.Where(x=>x.MetricsClassId==request.MetricsClassId);
            }
            //总条数
            var totalCount= metlist.Count();
            //总页数
            var totalPage=(int)Math.Ceiling(totalCount*1.0/ request.PagSize);
            //排序
            metlist = metlist.OrderByDescending(x => x.MetricsId).Skip((request.Pagindex-1)* request.PagSize).Take(request.PagSize);

            ApiPaging<List<MetricsDTO>> paging=new ApiPaging<List<MetricsDTO>>();
            paging.Data=metlist.ToList();
            paging.totalPage=totalPage;
            paging.totalCount=totalCount;
            result.Data=paging;
            return Task.FromResult(result);

        }
    }
}
